<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
    <script src="/js/socket.io.js"></script>
    <link rel='stylesheet' href='/stylesheets/style.css'/>
</head>
<body>
<ul id="message"></ul>
<input type="text" id="input">
<a href="javascript:" id="botton">发送</a>
<script>
    var ul = document.getElementById('message')
    var input = document.getElementById('input')
    var name = navigator.userAgent
    socket = io.connect('ws://192.168.2.105:3001/');
    socket.emit("message", {"name": name, "msg": "hello world"});
    socket.on("message", function (obj) {
        var li = document.createElement('li')
        li.innerHTML = obj.msg
        ul.appendChild(li)
        console.log(obj);
    });
    document.getElementById('botton').onclick = function () {
        if(input.value.length > 0){
            socket.emit("message", {"name": name, "msg": input.value});
            input.value = ''
        }
    }
</script>
</body>
</html>
